package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_coupon_detail;

import cn.com.api.service.ApiU_coupon_detailService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_coupon_detail/")
public class ApiU_coupon_detailController {
    @Autowired
    private ApiU_coupon_detailService apiU_coupon_detailService;
	
	private static Logger logger = Logger.getLogger(ApiU_coupon_detailController.class);
	
	/**
	 * 新增
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "insertU_coupon_detail")
	public void insertU_coupon_detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String coupon_detail_id = request.getParameter("coupon_detail_id");
		String coupon_id = request.getParameter("coupon_id");
		String order_no = request.getParameter("order_no");
		String user_id = request.getParameter("user_id");
		String use_time = request.getParameter("use_time");
		String status = request.getParameter("status");
		String is_balance = request.getParameter("is_balance");
		String balance_price = request.getParameter("balance_price");
		String balance_time = request.getParameter("balance_time");
		String coupon_code = request.getParameter("coupon_code");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");
        
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(coupon_detail_id!=null && !"".equals(coupon_detail_id.trim())){
				ApiU_coupon_detail u_coupon_detail = new ApiU_coupon_detail();
				
    		    //u_coupon_detail.setCoupon_detail_id(coupon_detail_id);
    		    //u_coupon_detail.setCoupon_id(coupon_id);
    		    //u_coupon_detail.setOrder_no(order_no);
    		    //u_coupon_detail.setUser_id(user_id);
    		    //u_coupon_detail.setUse_time(use_time);
    		    //u_coupon_detail.setStatus(status);
    		    //u_coupon_detail.setIs_balance(is_balance);
    		    //u_coupon_detail.setBalance_price(balance_price);
    		    //u_coupon_detail.setBalance_time(balance_time);
    		    //u_coupon_detail.setCoupon_code(coupon_code);
    		    //u_coupon_detail.setExt1(ext1);
    		    //u_coupon_detail.setExt2(ext2);
    		    //u_coupon_detail.setExt3(ext3);
    		    //u_coupon_detail.setExt4(ext4);
    		    //u_coupon_detail.setExt5(ext5);
				
				apiU_coupon_detailService.insertU_coupon_detail(u_coupon_detail);
				// return
                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_coupon_detailById")
	public void findU_coupon_detailById(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    String id = request.getParameter("id");
		    ApiU_coupon_detail u_coupon_detail = apiU_coupon_detailService.findU_coupon_detailById(Integer.valueOf(id));
		
			// return
            map.put("datas", u_coupon_detail);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
     /**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_coupon_details")
	public void findU_coupon_details(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<ApiU_coupon_detail> u_coupon_details = apiU_coupon_detailService.findU_coupon_details();
		
			// return
            map.put("datas", u_coupon_details);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	@RequestMapping(value = "updateU_coupon_detail")
	public void updateU_coupon_detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String coupon_detail_id = request.getParameter("coupon_detail_id");
		String coupon_id = request.getParameter("coupon_id");
		String order_no = request.getParameter("order_no");
		String user_id = request.getParameter("user_id");
		String use_time = request.getParameter("use_time");
		String status = request.getParameter("status");
		String is_balance = request.getParameter("is_balance");
		String balance_price = request.getParameter("balance_price");
		String balance_time = request.getParameter("balance_time");
		String coupon_code = request.getParameter("coupon_code");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");
        
        
		//String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(coupon_detail_id!=null && !"".equals(coupon_detail_id.trim())){
				ApiU_coupon_detail u_coupon_detail = new ApiU_coupon_detail();
				//获取原数据
				u_coupon_detail  = apiU_coupon_detailService.findU_coupon_detailById(Integer.valueOf(coupon_detail_id));
				//修改
				//u_coupon_detail.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiU_coupon_detailService.updateU_coupon_detail(u_coupon_detail);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		}
		catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
    
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "deleteU_coupon_detail")
	public void deleteU_coupon_detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
	
				Integer ret_I = apiU_coupon_detailService.deleteU_coupon_detail(Integer.parseInt(id));
				if(ret_I >0 ){
    				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
    				map.put(Utils.MESSAGE, "删除成功！");
                }else{
				    map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				    map.put(Utils.MESSAGE, "检查失败！");
			    }
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
 
}


